﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TelecoSystems.Modules.Products.ViewModels;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using TelecoSystems.Infrastructure;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Printing;
using ReflectionIT.Silverlight.ReportLibrary;
using System.Text;
using TelecoSystems.Infrastructure.Views;

namespace TelecoSystems.Modules.Products.Views {
    public partial class ProductsBrowseView : UserControl {
        protected ProductsBrowseViewModel ViewModel {
            get {
                return (ProductsBrowseViewModel)DataContext;
            }
        }
        public ProductsBrowseView(ProductsBrowseViewModel _viewModel) {
            InitializeComponent();

            DataContext = _viewModel;
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "PNG Files (*.png)|*.png|JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg|All Files (*.*)|*.*";
            dlg.FilterIndex = 1;

            if (dlg.ShowDialog() ?? false) {
                LoadImageFromFile(ProductImage, dlg.File);
            }
        }

        private void StackPanel_Drop(object sender, DragEventArgs e) {            
            //Check if user is dragging some data
            if (e.Data != null) {
                // Parse array of files
                FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
                if (files.Length > 0) {
                    //Get image from file and place it into ProductImage
                    FileInfo file = files[0];
                    List<string> allowedImageExt = new List<string>() { ".png", ".jpg", ".jpeg" };
                    if (allowedImageExt.Contains(file.Extension.ToLower())) {
                        LoadImageFromFile(ProductImage, file);
                    }
                }
            }

            ((StackPanel)sender).Background = new SolidColorBrush(Colors.Transparent);
        }

        private void LoadImageFromFile(Image targetImage, FileInfo file) {
            Stream stream = file.OpenRead();

            BinaryReader binary = new BinaryReader(stream);
            Byte[] bytes = binary.ReadBytes((int)stream.Length);
            ProductImage.Tag = bytes;

            stream.Position = 0;
            BitmapImage bi = new BitmapImage();
            bi.SetSource(stream);
            targetImage.Source = bi;
            stream.Close();
        }

        private void StackPanel_DragEnter(object sender, DragEventArgs e) {
            //Notify user visually           
            ((StackPanel)sender).Background = new SolidColorBrush(Color.FromArgb(100, 250, 243, 106));
        }
        private void StackPanel_DragLeave(object sender, DragEventArgs e) {
            ((StackPanel)sender).Background = new SolidColorBrush(Colors.Transparent);
        }
    }
}
